﻿using System;
using System.IO;
using System.Web;
using System.Text.RegularExpressions;
using SyCODE.Component.Security;
using SyCODE.AjaxUpload.Providers;
using SyCODE.Component;
using System.Web.Security;

namespace SyCODE.Blog.Uploader
{
    /// <summary>
    /// 上传文件完整保存名称的实现
    /// </summary>
    public class FileNameGenerator : ICustomFileNameGenerator
    {
        /// <summary>
        /// 根据登录获得完整的文件保存名称
        /// </summary>
        /// <param name="filename">上传文件的文件名</param>
        /// <returns></returns>
        public string GenerateFileName(string filename)
        {

			Ticket ticket = Ticket.Get(FormsAuthentication.FormsCookieName);
            //登录检测
            if (HttpContext.Current.Request.QueryString["uploadPath"] != "/" && Ticket.Authenticate(ticket))
            {
                filename = Regex.Replace(filename, ".jpeg\\s*$", ".jpg", RegexOptions.IgnoreCase);
                string uploadPath = HttpContext.Current.Request.QueryString["uploadPath"].Replace("../", "").Trim('/') + "/";

                if (String.Compare(uploadPath, "album", true) == 0 && filename.Length > 45)
                {
                    string extName = Path.GetExtension(filename);
                    filename = Function.StringCut(filename, 40).TrimEnd('.') + extName;
                }

                //相册图片的类型限制
                Regex regex1 = new Regex(".(jpg|gif|png|bmp)\\s*$", RegexOptions.IgnoreCase);
                //头像文件的类型限制
                Regex regex2 = new Regex(".(jpg|gif|png)\\s*$", RegexOptions.IgnoreCase);

                if ((String.Compare(uploadPath, "face", true) != 0 || regex1.IsMatch(filename)) && (String.Compare(uploadPath, "album", true) != 0 || regex1.IsMatch(filename)) && (String.Compare(uploadPath, "head", true) != 0 || regex2.IsMatch(filename)))
                {
                    string savePath = Path.Combine(AppPath.GetUserPhysicalPath(ticket.Name), uploadPath.Replace('/', '\\'));

                    if (!Directory.Exists(savePath))
                        Directory.CreateDirectory(savePath);

                    return savePath + filename;
                }
                else
                {
                    throw new InvalidDataException(StringResource.GetField("invitedfiletype"));
                }
            }
            else
            {
                ticket = Ticket.Get("manager");
                if (Ticket.Authenticate(ticket))
                {
                    string uploadPath = HttpContext.Current.Request.QueryString["uploadPath"].Replace("../", "").Trim('/');
                    string savePath = Path.Combine(AppPath.GetAppRootPath() + "u\\admin\\", uploadPath.Replace('/', '\\'));
                    if (!Directory.Exists(savePath))
                        Directory.CreateDirectory(savePath);

                    return savePath + filename;
                }
                else
                {
                    throw new System.Web.HttpRequestValidationException(StringResource.GetField("authfaild"));
                }
            }
        }
    }
}